package com.myapp.ruyixing.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.myapp.ruyixing.model.FoodList;
import com.myapp.ruyixing.model.FoodList.Food;

public class FoodListParser extends RYJSONParser {
	@Override
	public Object parse(JSONObject jsonObject) {
		if (getErrno(jsonObject) != NO_ERROR) {
			return null;
		}

		FoodList foodList = new FoodList();
		foodList.list = new ArrayList<FoodList.Food>();

		JSONObject data = jsonObject.optJSONObject("data");
		foodList.pn = data.optInt("pn");
		foodList.rn = data.optInt("rn");
		foodList.total = data.optInt("total");

		JSONArray list = data.optJSONArray("list");

		for (int i = 0; list != null && i < list.length(); i++) {
			JSONObject object = list.optJSONObject(i);
			if (object != null) {

				Food food = new Food();
				food.desc = object.optString("desc", "");
				food.distance = object.optLong("distance", 0);
				food.id = object.optString("id", "");
				food.name = object.optString("name", "");
				food.picurl = object.optString("picurl", "");
				food.restaurant = object.optString("restaurant", "");
				food.score = object.optInt("score", 0);
				food.address = object.optString("address", "");
				food.price = object.optString("price", "");

				foodList.list.add(food);
			}
		}

		return foodList;

	}
}
